//Detect browser LANG
var browserLang = "en";
try {
    browserLang = (navigator.language) ? navigator.language : navigator.userLanguage;
    browserLang = browserLang.substr(0, 2).toLowerCase();
    if (browserLang != "es" && browserLang != "en")
        browserLang = "en";
}
catch (e) { }
// END detect

define([
    'models/security/session',
    'models/i18n/login-' + browserLang,
	'text!templates/security/login.html',
    'text!templates/common/genericOptions.html'
], function (Session, Lang, loginTemplate, Options) {

    setLanguageDictionary(new Lang());

    var LoginView = Backbone.View.extend({
        el: $("#content"),
        events: {
            "submit #frmLogin": "login"
        },
        render: function () {
            
            var template = Handlebars.compile(loginTemplate);
            var html = template();

            this.$el.addClass("login");
            this.$el.html(html);
            postRender(this.$el);

            if ($.cookie('application_username')) {
                $("input[name='user']").val($.cookie('application_username'));
                $("input[name='pass']").focus();
            }
            else {
                $("input[name='user']").focus();
            }

            //BORRAR: autologin.. esto es de comodo nomas
            $("input[name='user_name']").val("super");
            $("input[name='user_pass']").val("123456");
            //$("#frmLogin").submit();
            // FIN BORRAR
        },
        login: function () {

            // ver imgLoading, si pongo una general para la app.. ( o si ya trae una )
            $("#imgLoading").show();
            $("#frmLogin button[type='submit']").attr("disabled", "disabled");
            //$("#frmLogin input[type='submit']").attr("disabled", "disabled");
            //$("#loginError").html("");
            //$(".alert").removeClass("hide").addClass("show");


            var session = new Session();
            //$("#frmLogin .user").val()
            var user = $("#frmLogin input[name='user_name']").val();
            var pass = $("#frmLogin input[name='user_pass']").val();
            var company = -1;//$("select[name='company']").val();
            session.connect(user, pass, company,
                    function (data) {

                        //save the cookie
                        if ($("input[name=remember_user_chk]").is(":checked")) {
                        //if ($("#remember").is(":checked")) {

                            $.cookie('application_username', user, { expires: 20 });
                        }else{
                            $.removeCookie('application_username');
                        }

                        require(['controllers/home/home'], function (Home) {
                            (new Home()).showApp();
                        });

                    },
                    function (err) {
                        showMessage("error", translate("_userOrPasswordAreIncorrect"));
                        $("#frmLogin button[type='submit']").prop("disabled", false);
                        /*
                        $("#imgLoading").hide();
                        $("#frmLogin input[type='submit']").removeAttr("disabled");

                        switch (err) {
                            case -1:
                                $("#loginError").html(translate("user_not_found"));
                                break;

                            case 0:
                                $("#loginError").html(translate("user_inactive"));
                                break;

                            case 2:
                                // si el usuario posee mas de una compania asociada
                                session.getUserCompanies(user, pass, function (result) {

                                    var data = {};
                                    data.options = [];

                                    for (var nn = 0; nn < result.length; nn++) {
                                        data.options.push({ value: result[nn].companyId, name: result[nn].name });
                                    }
                                    var optionsTemplate = Handlebars.compile(Options);
                                    var html = optionsTemplate(data);
                                    $("select[name='company']").addClass("select2-me").html(html);
                                    select2('#frmLogin');


                                    $('div.login-body div.forget').hide();
                                    $('#frmLogin div.control-group').hide();
                                    $('#frmLogin div.remember').hide();
                                    $("#frmLogin input[type='submit']").val(translate("select"));


                                    $('div.company-selector').show();
                                });




                                break;
                        }
                        */
                        return false;
                    });
            return false;
        }
    });
    return LoginView;


});


// script del template: account.js

//$(function () {

//    if ($('body').attr('data-page') == 'login' || $('body').attr('data-page') == 'signup' || $('body').attr('data-page') == 'password') {

//        /*  For icon rotation on input box foxus  */
//        $('.input-field').focus(function () {
//            $('.page-icon img').addClass('rotate-icon');
//        });

//        /*  For icon rotation on input box blur  */
//        $('.input-field').blur(function () {
//            $('.page-icon img').removeClass('rotate-icon');
//        });
//    };

//    /*  Background slide for lockscreen page  */
//    if ($('body').attr('data-page') == 'lockscreen') {
//        $.backstretch(["assets/img/background/01.png", "assets/img/background/02.png", "assets/img/background/03.png", "assets/img/background/04.png", "assets/img/background/05.png", "assets/img/background/06.png",
//		  "assets/img/background/07.png", "assets/img/background/08.png", "assets/img/background/09.png"],
//		  {
//		      fade: 600,
//		      duration: 4000
//		  });
//    }

//});